package client;

import org.apache.commons.cli.*;

import java.util.Scanner;

public class CmdClient {
    private client.SocketClient client;

    public CmdClient(client.SocketClient client) {
        this.client = client;
    }

    /**
     * 启动交互模式，允许用户在控制台循环输入命令。
     */
    public void startInteractiveMode() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入命令 (set <key> <value>, get <key>, rm <key>, exit)：");

        while (true) {
            System.out.print("$ ");
            String input = scanner.nextLine();
            String[] commandParts = input.split(" ");

            switch (commandParts[0]) {
                case "set":
                    if (commandParts.length == 3) {
                        client.set(commandParts[1], commandParts[2]);
                        System.out.println("数据添加成功！");
                    } else {
                        System.err.println("使用方法: set <key> <value>");
                    }
                    break;
                case "get":
                    if (commandParts.length == 2) {
                        String value = client.get(commandParts[1]);
                        if (value != null) {
                            System.out.println("键 \"" + commandParts[1] + "\" 的值为: " + value);
                        }
                    } else {
                        System.err.println("使用方法: get <key>");
                    }
                    break;
                case "rm":
                    if (commandParts.length == 2) {
                        client.rm(commandParts[1]);
                        System.out.println("键 \"" + commandParts[1] + "\" 已被移除。");
                    } else {
                        System.err.println("使用方法: rm <key>");
                    }
                    break;
                case "exit":
                    System.out.println("退出程序...");
//                    client.exit();
                    System.exit(0);
                    break;
                default:
                    System.err.println("命令错误: " + commandParts[0] + "请重新输入");
                    break;
            }
        }
    }


}